package com.springboot2.controller;

import com.springboot2.entity.Role;
import com.springboot2.service.RoleService;
import com.springboot2.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role/")
public class RoleController {

    @Resource
    private RoleService roleService;
    //增
    @PostMapping("add")
    public Result save(@RequestBody Role role){
        roleService.add(role);
        return Result.success();
    }
    //删
    @DeleteMapping("del/{id}")
    public Result del(@PathVariable int id){
        roleService.del(id);
        return Result.success();
    }
    //改
    @PutMapping("update")
    public Result update(@RequestBody Role role){
        roleService.update(role);
        return Result.success();
    }
    //获取对象
    @GetMapping("findById/{id}")
    public Result findById(@PathVariable int id){
        return Result.success(roleService.findById(id));
    }
    //分页列表
    @GetMapping("findByPage/{pageNo}/{pageSize}")
    public Result findByPage(@PathVariable int pageNo,@PathVariable int pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("data",roleService.findByPage(pageNo, pageSize));
        map.put("total",roleService.count());
        return Result.success(map);
    }
    //查询条件
    @GetMapping("findByPageParams/{pageNo}/{pageSize}/{name}")
    public Result findByPageParams(@PathVariable int pageNo,@PathVariable int pageSize,@PathVariable String name){
        Map<String,Object> map = new HashMap<>();
        map.put("data",roleService.findByPageParams(pageNo, pageSize, name));
        map.put("total",roleService.countParams());
        return Result.success(map);
    }
    //批量删除
    @DeleteMapping("batch/{ids}")
    public Result dels(@PathVariable String ids){
        String [] splits = ids.split(",");
        for (String split : splits){
            roleService.del(Integer.parseInt(split));
        }
        return Result.success();
    }
    //角色分配菜单
    @PostMapping("roleMenu/{roleId}")
    public Result roleMenu(@PathVariable Integer roleId, @RequestBody List<Integer> menuIds){
        roleService.setRoleMenu(roleId,menuIds);
        return Result.success();
    }
    //角色获取分配到的菜单
    @GetMapping("findRoleMenu/{roleId}")
    public Result findRoleMenu(@PathVariable Integer roleId){
        return Result.success(roleService.findByRoleMenu(roleId));
    }
    //角色获取分配到的菜单
    @GetMapping("list")
    public Result listsAll(){
        List<Role> list = roleService.findAll();
        return Result.success(list);
    }

    //角色分配资源
    @PostMapping("roleResource/{roleId}")
    public Result roleResource(@PathVariable Integer roleId, @RequestBody List<Integer> resourceIds){
        roleService.setRoleResource(roleId,resourceIds);
        return Result.success();
    }

    //角色获取分配到的资源
    @GetMapping("findRoleResource/{roleId}")
    public Result findRoleResource(@PathVariable Integer roleId){
        return Result.success(roleService.findByRoleResource(roleId));
    }


}
